/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webservices;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HttpsURLConnection;

/**
 *
 * @author BENAMMAR Riyadh
 */
public class Ville {
    
    String nom;
    double longetude;
    double latitude;

    public String getNom() {
        return nom;
    }

    public double getLongetude() {
        return longetude;
    }

    public double getLatitude() {
        return latitude;
    }
    
    public Ville(String ville){
        nom=ville;
        try {
            URL url=new URL("https://maps.googleapis.com/maps/api/geocode/xml?address="+nom+"&sensor=true&key=AIzaSyBGgPy7QrsfuuzxMvabaz8aG9NjiY25vJ8");
            HttpsURLConnection https=(HttpsURLConnection) url.openConnection();
            BufferedReader br=new BufferedReader(new InputStreamReader(https.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                if(line.contains("<lat>")){
                    line=line.replaceAll("<lat>", "");
                    line=line.replaceAll("</lat>", "");                    
                    latitude=Double.parseDouble(line);
                    
                }
                if(line.contains("<lng>")){                    
                    line=line.replaceAll("<lng>", "");
                    line=line.replaceAll("</lng>", "");
                    longetude=Double.parseDouble(line);
                    break;
                }
                
            }
            System.out.println("lat: "+latitude+" long: "+longetude);
        } catch (MalformedURLException ex) {
            Logger.getLogger(Ville.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Ville.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void main(String[] args) {
        Ville ville=new Ville("Paris");
    }
    
}
